package com.neisha.kit;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	
	public static String md5(String txt){
		String result = null;
		//用来将字节转换成 16 进制表示的字符
		char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; 
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte [] source = txt.getBytes("utf-8");
			md.update(source);
			
			byte[] tmp = md.digest();
			
			//用16进制数表示需要32位
            char[] str = new char[32];
            for(int i=0,j=0; i < 16; i++){
                //j表示转换结果中对应的字符位置
                //从第一个字节开始，对 MD5 的每一个字节
                //转换成 16 进制字符
                byte b = tmp[i];
                //取字节中高 4 位的数字转换
                //无符号右移运算符>>> ，它总是在左边补0
                //0x代表它后面的是十六进制的数字. f转换成十进制就是15
                str[j++] = hexDigits[b>>>4 & 0xf];
                // 取字节中低 4 位的数字转换
                str[j++] = hexDigits[b&0xf];
            }
            result = new String(str);//结果转换成字符串用于返回
		} catch (Exception e) {
			result = "";
		}
        return result;
	}
	
	
	public static String sha1(String txt){
		return sha(txt, "SHA-1");
	}
	
	public static String sha256(String txt){
		return sha(txt,"SHA-256");
	}
	
	private static String sha(String txt,String enc){
		 try {
			 MessageDigest md = MessageDigest.getInstance(enc);
		 	 byte[] digest = md.digest(txt.toString().getBytes());
		 	 return byteToStr(digest);
		 } catch (NoSuchAlgorithmException e) {
			 return "";
		 }
	}
	
	private static char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a','b', 'c', 'd', 'e', 'f' };
	private static String byteToStr(byte [] src){
		StringBuilder temp = new StringBuilder();
	 	for(int i =0;i<src.length;i++){
	 		char[] tempArr = new char[2];
	 		tempArr[0] = Digit[(src[i] >>> 4) & 0X0F];
	 		tempArr[1] = Digit[src[i] & 0X0F];
	 		temp.append(new String(tempArr));
	 	}
	 	return temp.toString();
	}
	
	
	public static String rc4(String aInput,String aKey){
        int[] iS = new int[256];   
        byte[] iK = new byte[256];   
        for (int i=0;i<256;i++){
            iS[i]=i;   
        }
        int j = 1;   
        for (short i= 0;i<256;i++){   
            iK[i]=(byte)aKey.charAt((i % aKey.length()));   
        }   
        j=0;   
        for (int i=0;i<255;i++){   
            j=(j+iS[i]+iK[i]) % 256;   
            int temp = iS[i];   
            iS[i]=iS[j];   
            iS[j]=temp;   
        }
        int i=0;   
        j=0;   
        char[] iInputChar = aInput.toCharArray();   
        char[] iOutputChar = new char[iInputChar.length];   
        for(short x = 0;x<iInputChar.length;x++){   
            i = (i+1) % 256;   
            j = (j+iS[i]) % 256;   
            int temp = iS[i];   
            iS[i]=iS[j];   
            iS[j]=temp;   
            int t = (iS[i]+(iS[j] % 256)) % 256;   
            int iY = iS[t];   
            char iCY = (char)iY;   
            iOutputChar[x] =(char)( iInputChar[x] ^ iCY) ;      
        }   
        return new String(iOutputChar);
	}
	
	
	
	public static void main(String[] args) {
		System.out.println(rc4("w3rslsdf","sdfsdfdsf"));
	}
}
